package com.zff.tool;
import com.github.wxpay.sdk.WXPayUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * TODO
 * @version 1.0
 * @author Zff
 * @date 2020/10/29 8:45
 */
public class MyWXPayUtil {
    // appid
    private String appid="wxcaac6ca7f336af1a"; // 这里填写你自己的appid
    // 商户号
    private String partner="1532188981"; // 这里填写你自己的商户号//语文报社财务部
    // 商户秘钥
    private String partnerkey="12345678901234567890123456789012"; // 这里填写你自己的商户秘钥
    // 回调地址
    private String notifyurl = "https://xspx.zhyww.cn/"; // 回调地址随便外网能访问就行
    // 统一下单接口
    private String url = "https://api.mch.weixin.qq.com/pay/unifiedorder";

    /**
     * 调起生成支付二维码
     * @param out_trade_no 自己生成的订单号
     * @param total_fee 总支付金额
     * @Aouthor:jin gang
     * @return
     */
    public Map createNavite(String out_trade_no,String total_fee,String good_id){
        Map<String,String> hashMap = new HashMap<>();
        hashMap.put("appid",appid);
        hashMap.put("mch_id",partner);
        hashMap.put("nonce_str", WXPayUtil.generateNonceStr());
        hashMap.put("body","语文报社");
        hashMap.put("out_trade_no",out_trade_no);
        hashMap.put("total_fee",total_fee);
        hashMap.put("spbill_create_ip","127.0.0.1");
        hashMap.put("notify_url",notifyurl);
        hashMap.put("trade_type","NATIVE");
        hashMap.put("product_id",good_id);
        try {
            // 将参数转成xml发送
            String xmlParam = WXPayUtil.generateSignedXml(hashMap, partnerkey);
            // 创建https请求，调用统一支付接口
            HttpClient httpClient = new HttpClient(url);
            httpClient.setHttps(true);
            httpClient.setXmlParam(xmlParam);
            httpClient.post();
            // 获取调用结果
            String content = httpClient.getContent();
            Map<String, String> stringStringMap = WXPayUtil.xmlToMap(content);
            System.out.println("stringStringMap:"+stringStringMap);
            Map<String, String> map = new HashMap<>();
            map.put("code_url",stringStringMap.get("code_url"));
            map.put("total_fee",total_fee);
            map.put("out_trade_no",out_trade_no);
            map.put("result_code",stringStringMap.get("result_code"));
            return map;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }


// 不需要查询支付状态的伙伴可以去掉这这个方法。。。。。。。。。。。。。。。。。。。。。。。
    /**
     * 调起查询支付状态接口
     * @param out_trade_no
     * @Aouthor:jin gang
     * @return
     */
    public Map queryPay(String out_trade_no){
        Map<String, String> hashMap = new HashMap<>();
        hashMap.put("appid",appid);
        hashMap.put("mch_id",partner);
        hashMap.put("out_trade_no",out_trade_no);
        hashMap.put("nonce_str",WXPayUtil.generateNonceStr());
        String url = "https://api.mch.weixin.qq.com/pay/orderquery";
        try {
            String xmlParam = WXPayUtil.generateSignedXml(hashMap, partnerkey);
            HttpClient httpClient = new HttpClient(url);
            httpClient.setHttps(true);
            httpClient.setXmlParam(xmlParam);
            httpClient.post();
            String content = httpClient.getContent();
            Map<String, String> stringStringMap = WXPayUtil.xmlToMap(content);
            System.out.println("**************b:"+stringStringMap);
            return stringStringMap;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
}
